package com.ruoyi.common.utils.poi;

/**
 * @author : tianyu
 * @date : 2025-01-11 14:56
 * @Version: 1.0
 * @Desc : 处理session_detail的导出
 */

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Configuration
public class WordExportService {

    /**
     * 将 content 转换为 Word 文件
     *
     * @param sessionDetailMap 包含topic、abstract、keywords、content、references字段，从数据库中获取
     * @return Word 文件的字节数组
     * @throws IOException 如果发生 I/O 错误
     */
    public byte[] generatePaper(HashMap<String, String> sessionDetailMap) throws IOException {
        XWPFDocument document = new XWPFDocument();

        // 按顺序生成文档的各个部分
        generateTopic(sessionDetailMap, document);
        generateAbstract(sessionDetailMap, document);
        generateKeywords(sessionDetailMap, document);
        generateContent(sessionDetailMap, document);
        generateReferences(sessionDetailMap, document);

        // 将文档内容写入 ByteArrayOutputStream
        try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
            document.write(byteArrayOutputStream);
            return byteArrayOutputStream.toByteArray();
        }
    }

    // 生成标题
    private void generateTopic(HashMap<String, String> sessionDetailMap, XWPFDocument document) {
        String topic = sessionDetailMap.get("topic");
        if (topic != null && !topic.isEmpty()) {
            XWPFParagraph titleParagraph = document.createParagraph();
            titleParagraph.setAlignment(ParagraphAlignment.CENTER); // 居中
            XWPFRun titleRun = titleParagraph.createRun();
            titleRun.setText(topic);
            titleRun.setFontSize(20);  // 标题字号
            titleRun.setBold(true);  // 标题加粗
        }
    }

    // 生成摘要
    private void generateAbstract(HashMap<String, String> sessionDetailMap, XWPFDocument document) {
        String abstractText = sessionDetailMap.get("abstract");
        if (abstractText != null && !abstractText.isEmpty()) {
            XWPFParagraph abstractParagraph = document.createParagraph();
            abstractParagraph.setSpacingBefore(20);  // 摘要段前距
            XWPFRun abstractRun = abstractParagraph.createRun();
            abstractRun.setText("摘要: ");
            abstractRun.setBold(true);
            abstractRun.addBreak();
            abstractRun.setText(abstractText);
            abstractRun.setFontSize(12);
            abstractRun.setItalic(true);  // 摘要为斜体
        }
    }

    // 生成关键词
    private void generateKeywords(HashMap<String, String> sessionDetailMap, XWPFDocument document) {
        String keywords = sessionDetailMap.get("keywords");
        if (keywords != null && !keywords.isEmpty()) {
            XWPFParagraph keywordsParagraph = document.createParagraph();
            XWPFRun keywordsRun = keywordsParagraph.createRun();
            keywordsRun.setText("关键词: ");
            keywordsRun.setBold(true);
            keywordsRun.setText(keywords);
            keywordsRun.setFontSize(12);
        }
    }

    // 生成正文，包含层级结构
    private void generateContent(HashMap<String, String> sessionDetailMap, XWPFDocument document) {
        String content = sessionDetailMap.get("content");
        if (content != null && !content.isEmpty()) {
            // 分割内容为多个部分，假设每个新的一行表示一个新的部分
            String[] sections = content.split("\n");

            // 正则表达式匹配标题
            Pattern title1Pattern = Pattern.compile("^\\d+\\s+.*"); // 一级标题: 1, 2, 3...
            Pattern title2Pattern = Pattern.compile("^\\d+\\.\\d+\\s+.*"); // 二级标题: 1.1, 1.2, 1.3...
            Pattern title3Pattern = Pattern.compile("^\\d+\\.\\d+\\.\\d+\\s+.*"); // 三级标题: 1.1.1, 1.1.2...

            for (String section : sections) {
                Matcher matcher1 = title1Pattern.matcher(section);
                Matcher matcher2 = title2Pattern.matcher(section);
                Matcher matcher3 = title3Pattern.matcher(section);

                if (matcher1.matches()) {
                    // 生成一级标题
                    XWPFParagraph title1Paragraph = document.createParagraph();
                    title1Paragraph.setAlignment(ParagraphAlignment.LEFT); // 左对齐
                    XWPFRun title1Run = title1Paragraph.createRun();
                    title1Run.setText(section);
                    title1Run.setFontSize(16);  // 一级标题字号
                    title1Run.setBold(true);  // 一级标题加粗
                } else if (matcher2.matches()) {
                    // 生成二级标题
                    XWPFParagraph title2Paragraph = document.createParagraph();
                    title2Paragraph.setAlignment(ParagraphAlignment.LEFT); // 左对齐
                    XWPFRun title2Run = title2Paragraph.createRun();
                    title2Run.setText(section);
                    title2Run.setFontSize(14);  // 二级标题字号
                    title2Run.setBold(true);  // 二级标题加粗
                } else if (matcher3.matches()) {
                    // 生成三级标题
                    XWPFParagraph title3Paragraph = document.createParagraph();
                    title3Paragraph.setAlignment(ParagraphAlignment.LEFT); // 左对齐
                    XWPFRun title3Run = title3Paragraph.createRun();
                    title3Run.setText(section);
                    title3Run.setFontSize(12);  // 三级标题字号
                    title3Run.setBold(true);  // 三级标题加粗
                } else {
                    // 生成正文
                    XWPFParagraph contentParagraph = document.createParagraph();
                    contentParagraph.setIndentationLeft(20);  // 设置正文缩进
                    XWPFRun contentRun = contentParagraph.createRun();
                    contentRun.setText(section);
                    contentRun.setFontSize(12);  // 正文字号
                }
            }
        }
    }
    // 生成参考文献
    private void generateReferences(HashMap<String, String> sessionDetailMap, XWPFDocument document) {
        String references = sessionDetailMap.get("references");
        if (references != null && !references.isEmpty()) {
            XWPFParagraph referencesParagraph = document.createParagraph();
            referencesParagraph.setSpacingBefore(20);  // 参考文献段前距
            XWPFRun referencesRun = referencesParagraph.createRun();
            referencesRun.setText("参考文献:");
            referencesRun.setBold(true);
            referencesRun.setFontSize(12);

            // 参考文献内容
            XWPFParagraph refParagraph = document.createParagraph();
            XWPFRun refRun = refParagraph.createRun();
            refRun.setText(references);
            refRun.setFontSize(12);
        }
    }
}
